package com.shengde.jinwawaforleader.model.myJob.classList;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shengde.jinwawaforleader.HomeApplication;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.myJob.instance_message.Constant;
import com.shengde.jinwawaforleader.model.personalCenter.activity.EditBabyNameActivity;
import com.shengde.jinwawaforleader.model.personalCenter.activity.EditBabySexyActivity;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProBumenList;
import com.shengde.jinwawaforleader.network.protocolbean.ProTeacherInfo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

public class ModifyTeacherInfo extends BaseActivity {
    private static final String TAG = "AddTeacherActivity";
    private TextView tv_back, fill_add, tv_title;
    private TextView tv_xueli, tv_csrq, tv_rzsj, tv_bumen;

    private RelativeLayout ll_csrq, ll_rzsj, ll_xueli, ll_bumen;
    private Calendar c = null;
    private DatePickerDialog dialog = null;
    private String[] single_list = {"初中", "高中", "大专", "本科", "硕士", "博士"};
    private int xueli_index = 2;
    public List<ProBumenList.OfficeEntity> office;
    public String[] bumenList;
    private AlertDialog.Builder builder;
    private AlertDialog alertDialog;
    private int bumen_index = 0;
    private String classid;
    private TextView teacherInfo;  //教师信息
    private RelativeLayout relative_iv_job_name;  //职务
    private RelativeLayout relative_iv_frofession_name; //职务名称
    private RelativeLayout relative_iv_graduate_school;//毕业学校
    private RelativeLayout relative_iv_link_telephone; //联系方式
    private RelativeLayout relative_iv_teacher_type; //老师类型
    private RelativeLayout relative_iv_baby_sexy;  //性别
    private RelativeLayout relative_iv_name; //姓名

    private TextView tv_job_name;
    private TextView tv_frofession_name;
    private TextView tv_graduate_school;
    private TextView tv_link_telephone;
    private TextView tv_teacher_type;
    private TextView tv_sexy;
    private TextView tv_name;
    private String mEdtStringTeacherType;
    private String mEdtStringSexy;
    private ProTeacherInfo.TeacherEntity teacher;
    private String teacherId;

    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.activity_add_teacher);
        Intent intent = getIntent();
        teacherId = intent.getExtras().getString("teacherId");
        teacher = intent.getParcelableExtra("teacherentity");
    }

    @Override
    public void initView() {
        tv_back = (TextView) findViewById(R.id.tv_back);
        fill_add = (TextView) findViewById(R.id.fill_add);
        tv_title = (TextView) findViewById(R.id.tv_title);
        teacherInfo = (TextView) findViewById(R.id.textview_contact);


        ll_csrq = (RelativeLayout) findViewById(R.id.ll_csrq);
        ll_rzsj = (RelativeLayout) findViewById(R.id.ll_rzsj);
        ll_xueli = (RelativeLayout) findViewById(R.id.ll_xueli);
        ll_bumen = (RelativeLayout) findViewById(R.id.ll_bumen);
        tv_xueli = (TextView) findViewById(R.id.tv_xueli);
        tv_csrq = (TextView) findViewById(R.id.tv_csrq);
        tv_rzsj = (TextView) findViewById(R.id.tv_rzsj);
        tv_bumen = (TextView) findViewById(R.id.tv_bumen);

        relative_iv_name = (RelativeLayout) findViewById(R.id.relative_iv_name);
        relative_iv_baby_sexy = (RelativeLayout) findViewById(R.id.relative_iv_baby_sexy);
        relative_iv_teacher_type = (RelativeLayout) findViewById(R.id.relative_iv_teacher_type);
        relative_iv_link_telephone = (RelativeLayout) findViewById(R.id.relative_iv_link_telephone);
        relative_iv_graduate_school = (RelativeLayout) findViewById(R.id.relative_iv_graduate_school);
        relative_iv_frofession_name = (RelativeLayout) findViewById(R.id.relative_iv_frofession_name);
        relative_iv_job_name = (RelativeLayout) findViewById(R.id.relative_iv_job_name);

        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_sexy = (TextView) findViewById(R.id.tv_sexy);
        tv_teacher_type = (TextView) findViewById(R.id.tv_teacher_type);
        tv_link_telephone = (TextView) findViewById(R.id.tv_link_telephone);
        tv_graduate_school = (TextView) findViewById(R.id.tv_graduate_school);
        tv_frofession_name = (TextView) findViewById(R.id.tv_frofession_name);
        tv_job_name = (TextView) findViewById(R.id.tv_job_name);


    }

    @Override
    public void initData() {
        tv_title.setText(getString(R.string.change_teacher_info));
        fill_add.setText(getString(R.string.sure));
        teacherInfo.setText(getString(R.string.teacher_info));
        c = Calendar.getInstance();
        relative_iv_teacher_type.setVisibility(View.GONE);
        BaseProtocol.getInstance().getBumenListModifyTea(this, User.getSchoolId(this));
    }

    @Override
    public void initListener() {
        tv_back.setOnClickListener(this);
        fill_add.setOnClickListener(this);

        ll_csrq.setOnClickListener(this);
        ll_rzsj.setOnClickListener(this);
        ll_xueli.setOnClickListener(this);
        ll_bumen.setOnClickListener(this);

        relative_iv_name.setOnClickListener(this);
        relative_iv_baby_sexy.setOnClickListener(this);
        relative_iv_teacher_type.setOnClickListener(this);
        relative_iv_link_telephone.setOnClickListener(this);
        relative_iv_graduate_school.setOnClickListener(this);
        relative_iv_frofession_name.setOnClickListener(this);
        relative_iv_job_name.setOnClickListener(this);
    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.fill_add:
                if (TextUtils.isEmpty(tv_name.getText().toString())
                        || TextUtils.isEmpty(tv_link_telephone.getText().toString())
                        || TextUtils.isEmpty(tv_csrq.getText().toString())
                        || TextUtils.isEmpty(tv_rzsj.getText().toString())
                        || TextUtils.isEmpty(tv_bumen.getText().toString())) {
                    HomeApplication.showToastShort("带*号选项不能为空!");
                    break;
                }
                BaseProtocol.getInstance().modifyTeacherInfo(this,office.get(bumen_index).getOfficeid() + "",
                        tv_name.getText().toString().trim(), mEdtStringSexy, tv_csrq.getText().toString().trim()
                        , tv_rzsj.getText().toString().trim(), xueli_index + "", tv_graduate_school.getText().toString().trim()
                        , tv_frofession_name.getText().toString().trim(), tv_job_name.getText().toString().trim(), tv_link_telephone.getText().toString().trim()
                        , teacherId);
                finish();
                break;
            case R.id.ll_csrq:
                dialog = new DatePickerDialog(
                        this,
                        new DatePickerDialog.OnDateSetListener() {
                            public void onDateSet(DatePicker dp, int year, int month, int dayOfMonth) {
                                tv_csrq.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
                            }
                        },
                        c.get(Calendar.YEAR), // 传入年份
                        c.get(Calendar.MONTH), // 传入月份
                        c.get(Calendar.DAY_OF_MONTH) // 传入天数
                );
                dialog.show();
                break;
            case R.id.ll_rzsj:
                dialog = new DatePickerDialog(
                        this,
                        new DatePickerDialog.OnDateSetListener() {
                            public void onDateSet(DatePicker dp, int year, int month, int dayOfMonth) {
                                tv_rzsj.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
                            }
                        },
                        c.get(Calendar.YEAR), // 传入年份
                        c.get(Calendar.MONTH), // 传入月份
                        c.get(Calendar.DAY_OF_MONTH) // 传入天数
                );
                dialog.show();
                break;
            case R.id.ll_xueli:
                builder = new AlertDialog.Builder(this);
                builder.setTitle("学历");
                builder.setSingleChoiceItems(single_list, xueli_index, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String str = single_list[which];
                        tv_xueli.setText(str);
                        xueli_index = which;
                        dialog.dismiss();
                    }
                });
                alertDialog = builder.create();
                alertDialog.show();
                break;
            case R.id.ll_bumen:
                builder = new AlertDialog.Builder(this);
                builder.setTitle("部门");
                builder.setSingleChoiceItems(bumenList, bumen_index, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        String str = bumenList[which];
                        tv_bumen.setText(str);
                        bumen_index = which;
                        dialog.dismiss();
                    }
                });
                alertDialog = builder.create();
                alertDialog.show();
                break;
            case R.id.relative_iv_name:
                Intent intent = new Intent(this, EditBabyNameActivity.class);
                intent.putExtra("relative_iv_baby", "12");
                startActivityForResult(intent, Constant.REQUEST_CODE_ADD_TEACHER);
                break;
            case R.id.relative_iv_baby_sexy:
                intent = new Intent(this, EditBabySexyActivity.class);
                intent.putExtra("select_radio", "4");
                startActivityForResult(intent, Constant.REQUEST_CODE_SECLECT);
                break;
            case R.id.relative_iv_teacher_type:
                intent = new Intent(this, EditBabySexyActivity.class);
                intent.putExtra("select_radio", "5");
                startActivityForResult(intent, Constant.REQUEST_CODE_SECLECT);
                break;
            case R.id.relative_iv_link_telephone:
                intent = new Intent(this, EditBabyNameActivity.class);
                intent.putExtra("relative_iv_baby", "13");
                startActivityForResult(intent, Constant.REQUEST_CODE_ADD_TEACHER);
                break;
            case R.id.relative_iv_graduate_school:
                intent = new Intent(this, EditBabyNameActivity.class);
                intent.putExtra("relative_iv_baby", "14");
                startActivityForResult(intent, Constant.REQUEST_CODE_ADD_TEACHER);
                break;
            case R.id.relative_iv_frofession_name:
                intent = new Intent(this, EditBabyNameActivity.class);
                intent.putExtra("relative_iv_baby", "15");
                startActivityForResult(intent, Constant.REQUEST_CODE_ADD_TEACHER);
                break;
            case R.id.relative_iv_job_name:
                intent = new Intent(this, EditBabyNameActivity.class);
                intent.putExtra("relative_iv_baby", "16");
                startActivityForResult(intent, Constant.REQUEST_CODE_ADD_TEACHER);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUEST_CODE_ADD_TEACHER) {
            if (resultCode == Constant.SETTING_PERSONAL_NAME_TWO) {
                String mEdtStringName = data.getExtras().getString("name_one");
                tv_name.setText(mEdtStringName);

            } else if (resultCode == Constant.SETTING_PERSONAL_LINK_TELEPHONE) {
                String mEdtStringLinkTelephone = data.getExtras().getString("link_telephone");
                tv_link_telephone.setText(mEdtStringLinkTelephone);

            } else if (resultCode == Constant.SETTING_GRADUTE_SCHOOL_SAVE_SUCCESS) {
                String mEdtStringLinkGraduteSchool = data.getExtras().getString("gradute_school");
                Log.i(TAG, "onActivityResult: mEdtStringLinkGraduteSchool =" + mEdtStringLinkGraduteSchool);
                tv_graduate_school.setText(mEdtStringLinkGraduteSchool);

            } else if (resultCode == Constant.SETTING_FROFRESSION_NAME_SAVE_SUCCESS) {
                String mEdtStringFrofessionName = data.getExtras().getString("frofession_name");
                Log.i(TAG, "onActivityResult: mEdtStringFrofessionName = " + mEdtStringFrofessionName);
                tv_frofession_name.setText(mEdtStringFrofessionName);


            } else if (resultCode == Constant.SETTING_JOB_NAME_SAVE_SUCCESS) {
                String mEdtStringJobName = data.getExtras().getString("job_name");
                tv_job_name.setText(mEdtStringJobName);

            }
            ;
        }

        if (requestCode == Constant.REQUEST_CODE_SECLECT) {
            if (resultCode == Constant.SETTING_SEXY_SAVE_SUCCESS) {
                mEdtStringSexy = data.getExtras().getString("mSexy");
                tv_sexy.setText(mEdtStringSexy);
                if ("男".equals(mEdtStringSexy)) {
                    mEdtStringSexy = "1";
                } else if ("女".equals(mEdtStringSexy)) {
                    mEdtStringSexy = "0";
                }

            } else if (resultCode == Constant.SETTING_TEACHER_TYPE_SAVE_SUCCESS) {
                mEdtStringTeacherType = data.getExtras().getString("mTeacherType");
                tv_teacher_type.setText(mEdtStringTeacherType);
                if (getString(R.string.teacher_of_class_number_one).equals(mEdtStringTeacherType)) {
                    mEdtStringTeacherType = "1";
                } else if (getString(R.string.teacher_of_class_number_two).equals(mEdtStringTeacherType)) {
                    mEdtStringTeacherType = "2";
                }
            }
        }
    }
     public void tianchongshuju(){
        if(teacher!=null) {
            try {
                tv_name.setText(teacher.getName());
                if("1".equals(teacher.getGender())) {
                    tv_sexy.setText("男");
                }else {
                    tv_sexy.setText("女");
                }
                SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
                tv_csrq.setText(format.format(teacher.getBirthDate()));
                tv_rzsj.setText(format.format(teacher.getEntryDate()));
                for(int i = 0; i < office.size(); i++) {
                    if(office.get(i).getOfficeid()==teacher.getInstitution_ID()) {
                        bumen_index = i;
                        tv_bumen.setText(office.get(i).getOffacename());
                    }
                }
                tv_link_telephone.setText(teacher.getCContact());
                xueli_index = Integer.parseInt(teacher.getEducationType());
                tv_xueli.setText(single_list[xueli_index]);
                tv_graduate_school.setText(teacher.getEducationUniversity());
                tv_frofession_name.setText(teacher.getTitle());
                tv_job_name.setText(teacher.getDuty());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
